/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.application.commands;

import com.nanosn.coffeemaker.Project;
import com.nanosn.coffeemaker.server.restlet.CoffeeMakerApplication;
import com.nanosn.util.parsers.commandline.Command;
import java.util.HashMap;
import org.restlet.Component;
import org.restlet.data.Protocol;

/**
 *
 * @author selkhateeb
 */
public class RunServer implements Command {

    @Override
    public void run(String[] args) throws Exception {
        try {
            // Compile sources ..
            new Compile().run(new String[]{});

            // Create a new Component.
            Component component = new Component();

            // Add a new HTTP server listening on port 8182.
            component.getServers().add(Protocol.HTTP, 8080);
            component.getClients().add(Protocol.FILE);

            // Attach the sample application.
            CoffeeMakerApplication coffeeMakerApplication = new CoffeeMakerApplication(Project.getClassLoader());

            //Context c = new Context();
            coffeeMakerApplication.setContext(component.getContext().createChildContext());

            if (coffeeMakerApplication.getContext().getAttributes() == null) {
                coffeeMakerApplication.getContext().setAttributes(new HashMap<String, Object>());
            }
            coffeeMakerApplication.getContext().getAttributes().put("RealPath", Project.getDevelopmentDirectory());

            coffeeMakerApplication.getContext().getAttributes().put("ProjectName", Project.getSettings().getRootPackage());

            component.getDefaultHost().attach(coffeeMakerApplication);

            // Start the component.
            component.start();
        }
        catch (Exception e) {
            // Something is wrong.
            e.printStackTrace();
        }
    }

    @Override
    public String getHelp() {
        return "Runs the embedded Server";
    }
}
